import requests
from .config_operate import read_ini_config

login_info = {}


def get_login_info(role: str, key: str, login_from="APP") -> str:
    # 根据传入的role判断登录的对象
    api = '/api/xxx/login'
    url = read_ini_config('Host', 'default_host') + api
    account = role + '_account'
    password = role + '_password'
    if account in login_info:
        return login_info[account][key]
    body = {
        "aaa": read_ini_config('Login', account),
        "bbb": read_ini_config('Login', password),
        "xxx": 0,
        "yyy": 1,
        "zzz": login_from
    }
    requests.DEFAULT_RETRIES = 5  # 增加重试连接次数
    s = requests.session()
    s.keep_alive = False  # 关闭多余连接
    data = s.post(url=url, json=body)
    result = data.json()
    if result['code'] == 200:
        login_info[account] = result['data']
        return result['data'][key]
    else:
        return result['message']
        

if __name__ == '__main__':
    pass
